package com.example.testnetty.utlis;


import java.text.DecimalFormat;
import java.util.Date;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;


// 数据 RGC格式转换
public class EnttyUtlis {
    /**
     * $GNGGA,035733.96,3032.7971040,N,10404.7303160,E,1,00,1.0,242.025,M,-42.025,M,0.0,0000*73..
     * @param lat
     * @param lon
     * @return
     */
    public static String buildGNGGA(double lat, double lon) {
        StringBuilder sb = new StringBuilder();
        sb.append("$GNGGA,");

        //世界协调时间
        sb.append(DateUtil.format(new Date(), "hhmmss.sss"));
        sb.append(",");

        DecimalFormat df = new DecimalFormat("#.0000000");
        //纬度绝对值和标识
        double latAbs = Math.abs(lat);
        int pre = (int) latAbs;//度
        double last = latAbs - pre;//分
        double v = last * 60 + pre*100;
        String latSymbol = "N";
        if (lat < 0) {
            latSymbol = "S";
        }
        String latStr = df.format(v);
        if (latStr.split("\\.")[0].length() < 4) {//如果位数不够就补0
            latStr = "0"+latStr;
        }
        sb.append(latStr);
        sb.append(",");
        sb.append(latSymbol);
        sb.append(",");

        //经度绝对值和标识
        double lonAbs = Math.abs(lon);
        int pre1 = (int) lonAbs;
        double last1 = lonAbs - pre1;
        double v1 = last1 * 60 + pre1*100;
        String lonSymbol = "E";
        if (lon < 0) {
            lonSymbol = "W";
        }
        String lonStr = df.format(v1);
        if (lonStr.split("\\.")[0].length() < 5) {//如果位数不够就补0
            lonStr = "0"+lonStr;
        }
        sb.append(lonStr);
        sb.append(",");
        sb.append(lonSymbol);

//        sb.append(",4,10,1,200,M,1,M,5,0");
//        sb.append(",4,10,1,200,M,1,M,5,0");
        sb.append(",4,00,1.0,242.025,M,-42.025,M,0.0,0000");
        String substring = sb.toString().substring(1, sb.length() );
        String check = getCheck(substring);
        sb.append("*"+check+"\r\n");
        return sb.toString();
    }
    public static String getCheck(String str) {
        char ch=str.charAt(0);
        int x=(int)ch;
        int y;
        for(int i=1;i<str.length();i++){
            y=(int)str.charAt(i);
            x=x^y;
        }
        String check= Integer.toHexString(x);
        return check;
    }

    public static String createNtripStr(String mountPoint, String user, String password){
        String msg = "GET /" + mountPoint +" HTTP/1.0 \r\n";// 请求行
        msg += "Host: rtk.ntrip.qxwz.com \r\n";// 请求首部
        msg += "Ntrip-Version: Ntrip/2.0 \r\n";// 通用首部
        msg += "User-Agent: NTRIP GNSSInternetRadio 1.4.10 \r\n";// 请求首部
        msg += "Accept: */*\r\nConnection: close\r\n";// 7802_RTD
        msg += "Accept:*/* \r\n";// 请求首部
//        msg += "Authorization: Basic " + Base64.encodeToString((user + ":" + password).getBytes(), 2);
        msg += "Authorization: Basic " + new String(Base64.encode((user + ":" + password).getBytes()))
                + "\r\n\r\n";// base64加密用户名和密码 // 请求首部
        return msg;}


}
